package com.hemaapp.yjnh.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

/**
 * 类描述：输入金额的EditText
 * 创建人：GuiyouZhang
 * 创建时间：2016/10/31 10:47
 * 修改备注：
 */
public class MoneyEditText extends EditText {
    private int afterDotLength = 2;// 小数点后的2位

    public MoneyEditText(Context context) {
        super(context);
        init();
    }

    public MoneyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MoneyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setAfterDotLength(int afterDotLength) {
        if (afterDotLength < 0)
            return;
        else if (afterDotLength == 0)
            this.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        else
            this.afterDotLength = afterDotLength;
    }

    private void init() {
        this.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        this.addTextChangedListener(new TextWatcher() {
            private boolean isChanged = false;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isChanged) {
                    return;
                }
                String str = s.toString();
                String dotBefore = "";// 小数点前的整数部分
                String dotAfter = "";// 小数部分
                isChanged = true;

                int dotIndex = str.indexOf(".");// 小数点的位置
                if (dotIndex > 0) {// 有小数点
                    dotBefore = str.substring(0, dotIndex);
                    dotAfter = str.substring(dotIndex + 1);
                } else {// 没有小数点
                    dotBefore = str;
                }
                // 删除整数部分开头的0
                while (dotBefore.length() > 1 && dotBefore.startsWith("0")) {
                    dotBefore = dotBefore.substring(1);
                }
                // 删除小数点后两位之后的数字
                if (dotAfter.length() > afterDotLength) {
                    dotAfter = dotAfter.substring(0, afterDotLength);
                }
                // 重新组合字符串
                if (dotIndex > 0) {// 有小数点
                    str = dotBefore + "." + dotAfter;
                } else if (dotIndex == 0) {// 如果输入的内容以小数点开头，就去掉小数点
                    str = "";
                } else {// 没有小数点
                    str = dotBefore;
                }
                int selection = getSelectionEnd();
                setText(str);
                // 设处理光标
                if (selection< length())
                    setSelection(selection);
                else
                    setSelection(length());
                isChanged = false;
            }
        });
    }
}
